/**
 * @author tibi
 */

 function EntityCreateForm(win, entity_tree){
 	var entityForm = new Ext.FormPanel({
		id				: 'myForm',
		standardSubmit	: false,
		title 			: 'TitluPanel',
		action			: 'server/EntitySave.php',
		width			: 'auto',
		width			: 'auto',
		autoScroll		: true,
		labelWidth		: 150,
		//height			: 500,
		//width			: 700,
		isUpload		: true,
		defaultType		: "textfield",
		items			: [
							{
								fieldLabel: 'Name',
								name: 'name',
								vtype: 'alpha',
								allowBlank: false
							},{
								fieldLabel: 'Label',
								name: 'label',
								//vtype: 'alphanum',
								allowBlank: false
							},{
								fieldLabel: 'Plural label',
								name: 'plural_label',
								//vtype: 'alphanum',
								allowBlank: false
							}
							],
		buttonAlign		: "center",
		buttons			:[
							new Ext.Button({
								text : 'Salveaza',
								handler: function() {
									entityForm.getForm().doAction('submit', {url:'server/EntityCreate.php'})
							    }
							    
							})
						],
		
	});
	
	entityForm.on({
        actioncomplete: function(form, action){
                            // Only enable the submit button if the load worked
            if(action.type == 'load'){
                submit.enable();
                Ext.MessageBox.alert('fs.actioncomplete', 'All OK.'); // Optional. Just for testing
            } 
            
            if(action.type == 'submit'){
                        // If the responseText is a null string, Ext doesnt raise an error so trap it here 
                        // as an error because it should be some json.
                if (action.response.responseText == '') {
					Ext.MessageBox.alert('fs.actioncomplete error', 'Form submit returned an empty string instead of json');
				}
				else {
					var result = Ext.decode(action.response.responseText);
					Ext.MessageBox.alert('Result', result.msg);
					win.close();
					entity_tree.root.reload();
				}
            }
        },
                        // The following code could be rationalised but I did it this way to make it easier
                        // to follow and for you to test it yourself.
                        // NOTE: the line var result = Ext.decode(action.response.responseText); must be
                        // called AFTER you have made sure there wasnt a failureType == "connect" or you 
                        // will probably get a JS error in the Ext library.
                            
        actionfailed: function(form,action){
            //alert('actionfailed');            
            if(action.type == 'load') { // Handle the LOAD errors
                if (action.failureType == "connect") { 
                    Ext.MessageBox.alert('fs.actionfailed error', 'Form load failed. Could not connect to server.');
                } else {
                    if (action.response.responseText != '') {
                        var result = Ext.decode(action.response.responseText);
                        if(result && result.msg) {            
                            Ext.MessageBox.alert('fs.actionfailed error', 'Form load failed with error: ' + action.result.msg);
                        } else {
                            Ext.MessageBox.alert('fs.actionfailed error', 'Form load failed with unknown error (possibly missing the "success" field in the json). Action type='+action.type+', failure type='+action.failureType);
                          }
                    } else {
                        Ext.MessageBox.alert('fs.actionfailed error', 'Form load returned an empty string instead of json');
                      }
                  }
            } 

            if(action.type == 'submit') { // Handle the SUBMIT errors
				
                if (action.failureType == "connect") { 
                    Ext.MessageBox.alert('fs.actionfailed error', 'Form submit failed. Could not connect to server.');
                } else 
                    if (action.failureType == "server") { 
                        // These arent "errors" as such, they are validation issues trapped by the server script and passed back for the user to correct
                    } else {    
                        var result = Ext.decode(action.response.responseText); 
                        if(result && result.msg) {            
                            Ext.MessageBox.alert('fs.actionfailed error', 'Form submit failed with error: ' + action.result.msg);
                        } else {
                            Ext.MessageBox.alert('actionfailed Error', 'Form submit returned unknown error. Action type='+action.type+', failure type='+action.failureType);
                          }
                      }
            }   
            
        } // end actionfailed listener
    }); // end fs.on
	return entityForm;
 }
